package com.gxa.service.impl;

import com.gxa.common.Constants;
import com.gxa.common.R;
import com.gxa.mapper.DictionaryMapper;
import com.gxa.pojo.Dictionary;
import com.gxa.service.DictionaryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @program: erp
 * @description: 字典service实现类
 * @author: LC
 * @create: 2021-12-06 11:10
 */

@Service
public class DictionaryServiceImpl implements DictionaryService {

    @Autowired(required = false)
    DictionaryMapper dictionaryMapper;

    @Override
    public R delDic(Dictionary dic) {
        R r=new R();
        int result = dictionaryMapper.delDic(dic.getDic_Name());
        if(result>0){
            r.setCode(200);
            r.setMsg(Constants.SUCCESS);
        }else{
            r.setCode(200);
            r.setMsg(Constants.FAILED);
        }
        return r;
    }

    @Override
    public R getChildDic(Dictionary dic) {
        R r=new R();
        List<Dictionary> dictionaries = dictionaryMapper.selectChildDic(dic.getDic_Name());
//        Map dictionaries = dictionaryMapper.selectChildDic(dic);
        System.out.println(dictionaries);
        r.setCode(200);
        r.setMsg(Constants.SUCCESS);
        r.setData(dictionaries);
        return r;
    }

    @Override
    public R modifyDic(Dictionary dic) {
        R r=new R();
        int result = dictionaryMapper.updateDic(dic);
        if(result>0){
            r.setCode(200);
            r.setMsg(Constants.SUCCESS);
        }else{
            r.setCode(200);
            r.setMsg(Constants.FAILED);
        }
        return r;
    }

    @Override
    public R getDic(Dictionary dic) {
        R r=new R();
        Dictionary dictionary = dictionaryMapper.selectDic(dic.getDic_Name());
        r.setCode(200);
        r.setMsg(Constants.SUCCESS);
        if(dictionary==null){
            r.setMsg(Constants.FAILED);
        }
        r.setData(dictionary);
        return r;
    }

    @Override
    public R addDic(Dictionary dic) {
        R r=new R();
        //判断重复
        if(dic.getParentDic()!=null){
            int count=dictionaryMapper.selectCount(dic.getDic_Name());
            System.out.println("数量"+count);
            if(count>0){
                //针对插入与已删除数据相同的名字导致不能添加的字典
//                //如果重复的是已删除的字典，只需恢复为常态，并按照传入的参数修改即可完成插入操作
//                Dictionary dictionary = dictionaryMapper.selectDic(dic.getDic_Name());
//                if(dictionary.getDic_Status()==Constants.DIC_DEL_STATUS){
//                    int result=dictionaryMapper.updateDic(dic);
//                    if(result>0){
//                        r.setCode(200);
//                        r.setMsg(Constants.SUCCESS);
//                    }else{
//                        r.setCode(200);
//                        r.setMsg(Constants.FAILED);
//                    }
//                    return r;
//                }
                r.setCode(200);
                r.setMsg("新增的字典重复，该字典已经存在");
                return r;
            }
            //通过名字查找父节点
            Dictionary parentDic=dictionaryMapper.selectDic(dic.getParentDic().getDic_Name());
            dic.setParentDic(parentDic);
        }
        //插入节点
        int result = dictionaryMapper.insertDic(dic);
        if(result>0){
            r.setCode(200);
            r.setMsg(Constants.SUCCESS);
        }else{
            r.setCode(200);
            r.setMsg(Constants.FAILED);
        }
        return r;
    }
}